{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Analytics Expression Language Example\n",
    "\n",
    "import xarray as xr\n",
    "from datetime import datetime\n",
    "from datacube.api import API\n",
    "from datacube.ndexpr import NDexpr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/rc/miniconda/lib/python2.7/site-packages/datacube-1.0.3+70.gc24cf61.dirty-py2.7.egg/datacube/api/_api.py:64: UserWarning: No application name set during API initialization\n",
      "  warnings.warn('No application name set during API initialization')\n"
     ]
    }
   ],
   "source": [
    "# Instantiating API and NDexpr\n",
    "\n",
    "g = API()\n",
    "nd = NDexpr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# construct data request parameters for band_30 and band_40\n",
    "data_request_descriptor = {\n",
    "    'platform': 'LANDSAT_5',\n",
    "    'product': 'nbar',\n",
    "    'variables': ('red', 'nir'),\n",
    "    'dimensions': {\n",
    "        'x': {\n",
    "            'range': (149.07, 149.18)\n",
    "        },\n",
    "        'y': {\n",
    "            'range': (-35.32, -35.28)\n",
    "        },\n",
    "        'time': {\n",
    "            'range': (datetime(1990, 1, 1), datetime(1990, 12, 31))\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "# Retrieving data from API\n",
    "d1 = g.get_data(data_request_descriptor)\n",
    "\n",
    "# construct data request parameters for PQ\n",
    "pq_request_descriptor = {\n",
    "    'platform': 'LANDSAT_5',\n",
    "    'product': 'pqa',\n",
    "    'variables': ('pixelquality'),\n",
    "    'dimensions': {\n",
    "        'x': {\n",
    "            'range': (149.07, 149.18)\n",
    "        },\n",
    "        'y': {\n",
    "            'range': (-35.32, -35.28)\n",
    "        },\n",
    "        'time': {\n",
    "            'range': (datetime(1990, 1, 1), datetime(1990, 12, 31))\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "# Retrieving data from API\n",
    "d2 = g.get_data(pq_request_descriptor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The following 3 lines shouldn't be done like this\n",
    "# Currently done like this for the sake of the example.\n",
    "\n",
    "b30 = d1['arrays']['red']\n",
    "b40 = d1['arrays']['nir']\n",
    "pq = d2['arrays']['pixelquality']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# NDexpr demo begins here\n",
    "\n",
    "# perform ndvi as expressed in this language.\n",
    "ndvi = nd.evaluate('((b40 - b30) / (b40 + b30))')\n",
    "\n",
    "# perform mask on ndvi as expressed in this language.\n",
    "masked_ndvi = nd.evaluate('ndvi{(pq == 32767) | (pq == 16383) | (pq == 2457)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray (time: 12, y: 231, x: 420)>\n",
      "dask.array<elemwis..., shape=(12, 231, 420), dtype=float64, chunksize=(1, 231, 420)>\n",
      "Coordinates:\n",
      "  * time     (time) datetime64[ns] 1990-03-02T23:11:16 1990-05-05T23:10:28 ...\n",
      "  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...\n",
      "  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...\n"
     ]
    }
   ],
   "source": [
    "print(ndvi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray (time: 12, y: 231, x: 420)>\n",
      "dask.array<elemwis..., shape=(12, 231, 420), dtype=float64, chunksize=(1, 231, 420)>\n",
      "Coordinates:\n",
      "  * time     (time) datetime64[ns] 1990-03-02T23:11:16 1990-05-05T23:10:28 ...\n",
      "  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...\n",
      "  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...\n"
     ]
    }
   ],
   "source": [
    "print(masked_ndvi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray (y: 231, x: 420)>\n",
      "array([[ 0.73212527,  0.73539463,  0.72473605, ...,  0.59518451,\n",
      "         0.60830706,  0.58306872],\n",
      "       [ 0.7266308 ,  0.73954829,  0.76156652, ...,  0.61830349,\n",
      "         0.60506634,  0.60726634],\n",
      "       [ 0.73212527,  0.7534574 ,  0.75202868, ...,  0.62756548,\n",
      "         0.63782567,  0.62214765],\n",
      "       ..., \n",
      "       [ 0.52543601,  0.5277012 ,  0.45971818, ...,  0.32512965,\n",
      "         0.33821287,  0.41319136],\n",
      "       [ 0.53044136,  0.54308146,  0.5111048 , ...,  0.42529602,\n",
      "         0.50864267,  0.46456217],\n",
      "       [ 0.55190161,  0.54160832,  0.54160832, ...,  0.54872513,\n",
      "         0.55118373,  0.48095632]])\n",
      "Coordinates:\n",
      "  * y        (y) float64 -3.957e+06 -3.957e+06 -3.957e+06 -3.957e+06 ...\n",
      "  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...\n"
     ]
    }
   ],
   "source": [
    "# currently dimensions are integer indices, later will be labels when\n",
    "# Data Access API Interface has been finalised.\n",
    "\n",
    "reduction_on_dim0 = nd.evaluate('median(masked_ndvi, 0)')\n",
    "print(reduction_on_dim0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray (x: 420)>\n",
      "array([ 0.64716312,  0.63130141,  0.61868247,  0.61391542,  0.60340276,\n",
      "        0.59467849,  0.60356416,  0.61881766,  0.62484235,  0.62029204,\n",
      "        0.62696296,  0.61071061,  0.60028249,  0.58451505,  0.58392989,\n",
      "        0.59976798,  0.59895753,  0.58481675,  0.5597446 ,  0.56663276,\n",
      "        0.56349213,  0.5696635 ,  0.57903731,  0.60062894,  0.59716351,\n",
      "        0.60839161,  0.61572052,  0.61572945,  0.60070671,  0.62942797,\n",
      "        0.63959854,  0.64738598,  0.6447136 ,  0.63424125,  0.6388767 ,\n",
      "        0.6495583 ,  0.63754395,  0.62567288,  0.60625   ,  0.59943708,\n",
      "        0.60211268,  0.5792613 ,  0.56115021,  0.55319901,  0.55046564,\n",
      "        0.56461676,  0.58592891,  0.59024691,  0.60006317,  0.60408163,\n",
      "        0.59771542,  0.59215686,  0.58400533,  0.57438273,  0.56755374,\n",
      "        0.56815044,  0.57192363,  0.58173665,  0.5995    ,  0.59690031,\n",
      "        0.5877974 ,  0.5800232 ,  0.57953568,  0.58183242,  0.57293002,\n",
      "        0.56092699,  0.5534309 ,  0.55106541,  0.54068027,  0.54573765,\n",
      "        0.56598392,  0.55711289,  0.55015353,  0.55052265,  0.55237187,\n",
      "        0.55528195,  0.54484415,  0.532831  ,  0.52224545,  0.50275739,\n",
      "        0.50289752,  0.49083769,  0.50091743,  0.54097497,  0.56345886,\n",
      "        0.57426836,  0.57289528,  0.55979705,  0.5446898 ,  0.54381271,\n",
      "        0.54380841,  0.537577  ,  0.54056902,  0.54695218,  0.56050339,\n",
      "        0.5786034 ,  0.56942699,  0.57164404,  0.57236992,  0.58119483,\n",
      "        0.58683927,  0.58144031,  0.56495222,  0.56258657,  0.5545761 ,\n",
      "        0.54836032,  0.54567157,  0.54101419,  0.52129826,  0.5209286 ,\n",
      "        0.52170062,  0.51807943,  0.51524208,  0.51232352,  0.51231306,\n",
      "        0.52056239,  0.52139274,  0.5216196 ,  0.52206545,  0.53163686,\n",
      "        0.52824579,  0.51158301,  0.50954774,  0.5198765 ,  0.52043597,\n",
      "        0.53378649,  0.53266996,  0.53168942,  0.55102041,  0.54919236,\n",
      "        0.53250672,  0.52972359,  0.53322658,  0.52563689,  0.51656426,\n",
      "        0.50839965,  0.50149155,  0.49312377,  0.48547718,  0.47518276,\n",
      "        0.48665049,  0.49213829,  0.49802251,  0.5153185 ,  0.52056239,\n",
      "        0.51597503,  0.51197987,  0.5146147 ,  0.51743028,  0.52623173,\n",
      "        0.53163686,  0.53127247,  0.52508361,  0.52914022,  0.52873563,\n",
      "        0.53138075,  0.52984798,  0.52469396,  0.53299492,  0.53427967,\n",
      "        0.53134153,  0.53045608,  0.53124083,  0.53198199,  0.53142451,\n",
      "        0.52868716,  0.52751596,  0.52503682,  0.54022754,  0.53963752,\n",
      "        0.53228457,  0.52390589,  0.51895999,  0.52982456,  0.52531663,\n",
      "        0.52499933,  0.53063498,  0.53583199,  0.5519893 ,  0.53715542,\n",
      "        0.51966122,  0.5212156 ,  0.51642285,  0.51660517,  0.51793655,\n",
      "        0.51826484,  0.5416936 ,  0.53145689,  0.51415168,  0.52459467,\n",
      "        0.50840955,  0.50395778,  0.51839179,  0.53116102,  0.5298826 ,\n",
      "        0.50453985,  0.49242612,  0.47327502,  0.4667115 ,  0.47486772,\n",
      "        0.47997497,  0.48341745,  0.48172222,  0.47122078,  0.45884225,\n",
      "        0.44631386,  0.42475172,  0.42110176,  0.43501611,  0.42806418,\n",
      "        0.44185788,  0.434238  ,  0.4536206 ,  0.46677698,  0.45930925,\n",
      "        0.4550723 ,  0.45623632,  0.4345791 ,  0.43142067,  0.42302853,\n",
      "        0.42801121,  0.41962422,  0.44075054,  0.44690759,  0.41405082,\n",
      "        0.4182135 ,  0.43485136,  0.45052131,  0.44150184,  0.43855435,\n",
      "        0.44452537,  0.45646357,  0.44329229,  0.42287695,  0.41632852,\n",
      "        0.44169847,  0.45090294,  0.4591594 ,  0.44799376,  0.42669969,\n",
      "        0.43229463,  0.41385768,  0.39332948,  0.38927419,  0.35374431,\n",
      "        0.32616131,  0.32741407,  0.30829651,  0.29519871,  0.29695493,\n",
      "        0.32944083,  0.33668045,  0.35165487,  0.38625239,  0.38733706,\n",
      "        0.38017899,  0.39831105,  0.40587219,  0.40977981,  0.41466128,\n",
      "        0.40884279,  0.3881726 ,  0.39249504,  0.37025316,  0.35394827,\n",
      "        0.33747064,  0.30133998,  0.29514172,  0.32187209,  0.35094851,\n",
      "        0.37355177,  0.37759336,  0.39525521,  0.36336279,  0.33257666,\n",
      "        0.32554434,  0.37358715,  0.38643285,  0.40918298,  0.44444444,\n",
      "        0.46147736,  0.47062484,  0.46      ,  0.472568  ,  0.47778315,\n",
      "        0.46334716,  0.44700738,  0.4476726 ,  0.46424266,  0.46426051,\n",
      "        0.47151899,  0.47515745,  0.47844154,  0.48316598,  0.49474212,\n",
      "        0.49309011,  0.50174564,  0.5009901 ,  0.51078176,  0.52142513,\n",
      "        0.5119852 ,  0.50694087,  0.50606594,  0.51318444,  0.50469748,\n",
      "        0.50779221,  0.51626831,  0.51701418,  0.50976157,  0.52216749,\n",
      "        0.52572442,  0.53087349,  0.54120417,  0.54075724,  0.54122237,\n",
      "        0.54449838,  0.54688618,  0.54801642,  0.55504056,  0.56627485,\n",
      "        0.57676903,  0.5744898 ,  0.56649534,  0.57063859,  0.57351027,\n",
      "        0.57749465,  0.5860367 ,  0.5852639 ,  0.57955205,  0.58061002,\n",
      "        0.58912769,  0.59786176,  0.58555217,  0.58601196,  0.58494005,\n",
      "        0.5839948 ,  0.58312236,  0.57715431,  0.56185176,  0.55892678,\n",
      "        0.57253046,  0.56723404,  0.55678836,  0.54387806,  0.53451811,\n",
      "        0.54870941,  0.54870941,  0.53721156,  0.52724662,  0.5195056 ,\n",
      "        0.53600661,  0.54413695,  0.55123452,  0.53219448,  0.53796321,\n",
      "        0.54551161,  0.54220711,  0.53588357,  0.53952647,  0.53164557,\n",
      "        0.514198  ,  0.51922476,  0.54727656,  0.56772152,  0.56002566,\n",
      "        0.55941499,  0.56093512,  0.57221352,  0.58122144,  0.60186047,\n",
      "        0.60988838,  0.61175711,  0.61261888,  0.61154983,  0.60847018,\n",
      "        0.60926993,  0.60947965,  0.60455587,  0.60764873,  0.61259588,\n",
      "        0.60863398,  0.60553633,  0.59170903,  0.58205901,  0.56641604,\n",
      "        0.56629393,  0.56648659,  0.56470588,  0.56173469,  0.55895647,\n",
      "        0.57115437,  0.58333333,  0.58121822,  0.57669307,  0.57529177,\n",
      "        0.57563641,  0.57962472,  0.58165865,  0.58155455,  0.57796327,\n",
      "        0.5893443 ,  0.59803729,  0.60252366,  0.59866716,  0.59035409,\n",
      "        0.59673469,  0.60523069,  0.60903285,  0.61407307,  0.61531449,\n",
      "        0.61168917,  0.61096178,  0.61711281,  0.62464722,  0.61994949,\n",
      "        0.61159227,  0.60675226,  0.60559346,  0.61357913,  0.61174596])\n",
      "Coordinates:\n",
      "  * x        (x) float64 1.545e+06 1.545e+06 1.545e+06 1.545e+06 1.545e+06 ...\n"
     ]
    }
   ],
   "source": [
    "reduction_on_dim01 = nd.evaluate('median(masked_ndvi, 0, 1)')\n",
    "print(reduction_on_dim01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray ()>\n",
      "array(0.5337864854058376)\n"
     ]
    }
   ],
   "source": [
    "reduction_on_dim012 = nd.evaluate('median(masked_ndvi, 0, 1, 2)')\n",
    "print(reduction_on_dim012)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
